package com.ruoyi.forApp.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.forApp.domain.Student;
import com.ruoyi.framework.shiro.service.LoginService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class StudentControllerForApp extends BaseController {
    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @PostMapping("/getStudentInfo")
    @ResponseBody
    public Map<String,Object> getStudentInfo(Long userId)
    {
        Map<String,Object> map=new HashMap<>();
        Student student=new Student();
        try
        {
            User user=userService.selectUserById(userId);
            student.setClassName(user.getDept().getDeptName());
            student.setEmail(user.getEmail());
            student.setLoginName(user.getLoginName());
            student.setPhonenumber(user.getPhonenumber());
            String sex=user.getSex();
            if (sex.equals("0")){
                sex="男";
            }else if (sex.equals("1")){
                sex="女";
            }else {
                sex="未知";
            }
            student.setSex(sex);
            student.setUserId(userId);
            String schoolName=deptService.findSchoolByDeptId(user.getDeptId());
            student.setSchoolName(schoolName);
            student.setUserName(user.getUserName());
            map.put("success",true);
            map.put("student",student);
            map.put("msg","查询成功");
        }
        catch (Exception e)
        {
            String msg = "无效的userId";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            map.put("success",false);
            map.put("msg",msg);
            map.put("student",student);

        }
        return map;
    }


    @PostMapping("/saveStudentInfo")
    @ResponseBody
    public Map<String,Object> saveStudentInfo(Long userId,String phone,String email)
    {
        Map<String,Object> map=new HashMap<>();
        User user=new User();
        user.setUserId(userId);
        user.setPhonenumber(phone);
        user.setEmail(email);
        int su=userService.updateUserInfo(user);
        if (su>0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }
}
